﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using PhoneAppSTS.Utils.Data;
using PhoneAppSTS.Utils;

namespace PhoneAppSTS
{
    public partial class ShowMedicines : PhoneApplicationPage
    {
        private MedicineDataUtils medicineDataUtils;
        public ShowMedicines()
        {
            ApplicationBar = App.AppBar;
            InitializeComponent();

            medicineDataUtils = new MedicineDataUtils();
            medicineDataUtils.MedicineList = new ObservableCollection<MedicineData>();
            medicineDataUtils.MedicinesForTakenList = new Dictionary<MedicineData, int>();
            this.DataContext = medicineDataUtils;
            
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            medicineDataUtils.DownloadMedicineData();
            

            string paramValue = "";
            NavigationContext.QueryString.TryGetValue("which", out paramValue);
            int intParamValue = 0;
            Int32.TryParse(paramValue,out intParamValue);
            
            FindMedicines(intParamValue);

            MedicineListBox.ItemsSource = medicineDataUtils.MedicinesForTakenList;
        }

        private void FindMedicines(int intParamValue)
        {
            foreach (var item in medicineDataUtils.MedicineList)
            {
                foreach (var time in item.TimeForMedicine)
                {
                    if (intParamValue == time.Key)
                    {
                        medicineDataUtils.MedicinesForTakenList.Add(item, time.Value);
                        //break;
                    }
                }
            }
        }
    }
}